package com.songshuai.webapi.login.controller;

import com.songshuai.webapi.baseapi.annotations.SysLogger;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.login.dto.ResetPasswordDto;
import com.songshuai.webapi.login.dto.RespDTO;
import com.songshuai.webapi.login.dto.UserLoginDto;
import com.songshuai.webapi.login.service.CrmLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Pattern;

@Api("CRM登录")
@Slf4j
@RestController
@RequestMapping("/crm/user")
public class CRMLoginController {

    @Autowired
    private CrmLoginService crmLoginService;

    @SysLogger(operation = "CRM用户登录",value = "crm_login")
    @ApiOperation(value = "CRM登录", notes = "username,password,type为必选项")
    @PostMapping("login")
    public RespDTO crmLogin(@RequestBody UserLoginDto user){
        //参数判读省略
        RespDTO respDTO = check(user);
        if(1==respDTO.code){
            return respDTO;
        }
        RespDTO dto = crmLoginService.crmLogin(user);
        return dto;
    }

    @GetMapping("getCode")
    @ApiOperation(value = "获取验证码", notes = "mobile为必选项")
    public RespDTO getVerificationCode(String mobile){
        if(StringUtils.isNotBlank(mobile)){
            return crmLoginService.sentVerificationCode(mobile);
        }else{
            return RespDTO.onFail("手机号不能为空");
        }
    }

    @ApiOperation(value = " 重置密码",notes = "mobile,code,newPwd为必填项")
    @PostMapping("resetPassword")
    public RespDTO resetPassword(@RequestBody ResetPasswordDto resetPasswordDto){
        if(null!=resetPasswordDto) {
            UserLoginDto user = new UserLoginDto();
            user.setType(2);
            user.setUsername(resetPasswordDto.getMobile());
            user.setPassword(resetPasswordDto.getCode());
            RespDTO dto = this.crmLogin(user);

            if (StringUtils.isNotBlank(resetPasswordDto.getNewPwd())) {
                String regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[\\s\\S]{6,20}$";//密码长度最低需要6位，需同时包含大写字母、小写字母和数字
                boolean matches = Pattern.matches(regexp, resetPasswordDto.getNewPwd());
                if(!matches){
                    return RespDTO.onFail("密码长度需要6-20位，需同时包含大写字母、小写字母和数字");
                }
            } else {
                return RespDTO.onFail("新密码不能为空");
            }

            if (0 == dto.code) {
                int i = crmLoginService.resetPassword(resetPasswordDto.getMobile(), resetPasswordDto.getNewPwd());
                if (i > 0) {
                    return RespDTO.onSuc(resetPasswordDto);
                } else {
                    return RespDTO.onFail("重置失败稍后重试");
                }

            } else {
                return dto;
            }
        }else{
            return RespDTO.onFail("请求体为空");
        }

    }


    RespDTO check(UserLoginDto user) {
        if (null == user) {
            return RespDTO.onFail("用户信息为空");
        }
        if(null==user.getType()){
            return RespDTO.onFail("登录类型为空");
        }
        if(StringUtils.isBlank(user.getUsername())){
            return RespDTO.onFail("用户名为空");
        }
        if(StringUtils.isBlank(user.getPassword())){
            return RespDTO.onFail("密码为空");
        }
        return RespDTO.onSuc(user);
    }

    
}
